package com.lq.educationsystem.student.service.impl;

import com.lq.educationsystem.common.constant.ResultContant;
import com.lq.educationsystem.common.entiy.*;

import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.*;
import com.lq.educationsystem.student.entiy.StudentTb;
import com.lq.educationsystem.student.feign.*;
import com.lq.educationsystem.student.mapper.StudentTbMapper;
import com.lq.educationsystem.student.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentTbMapper studentTbMapper;
    //导入ClassesStudentFeign

    //ClassesFeign Feign导入
    //调用班级服务feign
    @Autowired
    ClassesFeign classesFeign;
    //调用课程feign
    @Autowired
    SubjectFeign subjectFeign;
//调用teacherFeign
    @Autowired
    TeacherFeign teacherFeign;
//调用考试feign
    @Autowired
    ExaminationFeign examinationFeign;
//调用分数feign
    @Autowired
    ScoreFeign scoreFeign;

    @Override
    public Result<StudentTb> studentLogn(StudentLognVo studentLognVo) {
        //登录功能
        StudentTb studentTb = new StudentTb();
        BeanUtils.copyProperties(studentLognVo,studentTb);
        //查询数据库
        String studentAccount = studentTb.getStudentAccount();

        StudentTb studentInfo =  studentTbMapper.queryByAccount(studentAccount);

        if(studentInfo == null){
            Result result = ResultBuild.buildDefeat();
            result.setMessage("用户名不存在");
            return result;
        }
        //密码的判断
        if (studentInfo.getStudentPassword().equals(studentLognVo.getStudentPassword())) {
            //成功
            Result<StudentTb> studentTbResult = ResultBuild.buildOk(studentInfo);

            return studentTbResult;
        }else {
            //密码错误

            Result result = new Result();
            result.setCode(ResultContant.DEFEAT_RESULT.getCode());
            //设置失败
            result.setMessage("密码错误");
            return  result;
        }

    }

    /**
     * 通过学生id查询所在班级的同班同学的信息
     * @param studentPageVo
     * @return
     */

    @Override
    public ResultPage<StudentTb> queryClassesStudentListInfoByStudentId(@RequestBody StudentPageVo studentPageVo) {
        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        classesStudentTb.setStudentId(studentPageVo.getStudentId());
        //第一步通过学生id查询班级信息
        Result<ClassesStudentTb> classesStudentTbResult = classesFeign.queryClassesInfoByStudentId(classesStudentTb);

        Integer classesId = ((ClassesStudentTb) classesStudentTbResult.getData()).getClassesId();
        //第二步通过班级信息查询班级内学生的id

        //创建pageVo
        ClassesStudentPageVo classesStudentPageVo = new ClassesStudentPageVo();
        classesStudentPageVo.setPage(studentPageVo.getPage());
        classesStudentPageVo.setPageSize(studentPageVo.getPageSize());
        classesStudentPageVo.setClassesId(classesId);
        ResultPage<ClassesStudentTb> classesStudentTbResultPage = classesFeign.queryBatchClassesStudentInfoByStudentId(classesStudentPageVo);
        System.out.println("classesStudentTbResultPage = " + classesStudentTbResultPage);
        //收集学生id集合
        List<ClassesStudentTb> data = classesStudentTbResultPage.getData();
        System.out.println("ClassesStudentTb = " + data);
        List<Integer> collectClassesId = data.stream().map((item) -> {
            return item.getStudentId();
        }).collect(Collectors.toList());
        System.out.println("收集学生id的集合为:"+collectClassesId);

        //批量查询数据
        List<StudentTb> studentTbList = studentTbMapper.queryBatch(collectClassesId);
        //通过学生id的集合查询批量查询班级学生信息，并且返回给前端
        System.out.println("studentTbList = " + studentTbList);
        //数据取出密码
        List<StudentTb> collect = studentTbList.stream().map((item) -> {

            item.setStudentPassword("");

            return item;
        }).collect(Collectors.toList());


        return  ResultPageBuild.pageBuildOk(new ResultPage<StudentTb>(),studentPageVo.getPage(),studentPageVo.getPageSize(),classesStudentTbResultPage.getCountTotal(),collect);

    }

    @Override
    public ResultPage<ClassesSubjectTeacherVo> queryStudentSubjectInfo(StudentPageVo studentPageVo) {
        //第一步根据学生id查出班级id
        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        classesStudentTb.setStudentId(studentPageVo.getStudentId());
        //获取班级id
        Result<ClassesStudentTb> classesStudentTbResult = classesFeign.queryClassesInfoByStudentId(classesStudentTb);
        Integer classesId = ((ClassesStudentTb) classesStudentTbResult.getData()).getClassesId();

        System.out.println("classesStudentTbResult = " + classesStudentTbResult);
        //第二步根据班级id 查出classes_subject_tb 查出 teacher_subject_id
        ClassesSubjectPageVo classesSubjectPageVo = new ClassesSubjectPageVo();
        classesSubjectPageVo.setClassesId(classesId);
        //设置页数
        classesSubjectPageVo.setPage(studentPageVo.getPage());
        //设置取几个
        classesSubjectPageVo.setPageSize(studentPageVo.getPageSize());
        //这里的数据包括页面的相关设置
        ResultPage<ClassesSubjectTb> classesSubjectTbResultPage = classesFeign.querySubjectInfoListByClassesId(classesSubjectPageVo);
        System.out.println("classesSubjectTbResultPage = " + classesSubjectTbResultPage);
        List<ClassesSubjectTb> data = classesSubjectTbResultPage.getData();
        System.out.println("data = " + data);
        //第三步根据 teacher_subject_id 分别查出 teacher_id 和 subject_id 的信息  然后在分别查出信息 最后拼接到一起 返回给前端

        //获取 teacher_subject_id 信息集合
        List<Integer> collectTeacherSubjectIdListInfo = data.stream().map((item) -> {
            return item.getTeacherSubjectId();
        }).collect(Collectors.toList());
        //taecherSubjectId中表 teacher_id 和 subject_id 是一一对应的
        ResultPage<TeacherSubjectTb> teacherSubjectTbResultPage = subjectFeign.queryTeacherSubjectInfoByTeacherSubjectId(collectTeacherSubjectIdListInfo);


        //获取数据
        List<TeacherSubjectTb> teacherSubjectTbListInfo = teacherSubjectTbResultPage.getData();
        //分别查出teacher_id和subject_id

        //获取课程id
        List<Integer> collectSubjectIdListInfo = teacherSubjectTbListInfo.stream().map((item) -> {
            return item.getSubjectId();
        }).collect(Collectors.toList());
        //获取教师id
        List<Integer> collectTeacherIdList = teacherSubjectTbListInfo.stream().map((item) -> {
            return item.getTeacherId();
        }).collect(Collectors.toList());

        //批量查询数据
        //通过课程id集合批量查询数据
        ResultPage<SubjectTb> subjectTbResultPage = subjectFeign.queryBatchSubjectInfoBySubjectIdList(collectSubjectIdListInfo);
        List<SubjectTb> subjectInfoList = subjectTbResultPage.getData();
        //通过教师id批量查询数据
        ResultPage<TeacherTb> teacherTbResultPage = teacherFeign.queryBatchTeacherInfoByTeacherIdList(collectTeacherIdList);
        List<TeacherTb> teacherInfoList = teacherTbResultPage.getData();
        //通过teacherSubjectTbListInfo 查询 批量封装数据
/**
 * 两组数据拼装到一起后返回给前端
 * 只要返回给前端空值就是错了
 */
        ArrayList<ClassesSubjectTeacherVo> classesSubjectTeacherVos = new ArrayList<>();
        if(subjectInfoList.size() == teacherInfoList.size()){
            //使用for循环遍历组合在一起 这虽然不一定正确 先做出来吧
            for (int i = 0; i < subjectInfoList.size(); i++) {
                SubjectTb subjectTb = subjectInfoList.get(i);
                TeacherTb teacherTb = teacherInfoList.get(i);
                //数据组合
                ClassesSubjectTeacherVo classesSubjectTeacherVo = new ClassesSubjectTeacherVo();

                //数据设置
                classesSubjectTeacherVo.setSubjectId(subjectTb.getSubjectId());
                classesSubjectTeacherVo.setSubjectName(subjectTb.getSubjectName());

                classesSubjectTeacherVo.setTeacherId(teacherTb.getTeacherId());
                classesSubjectTeacherVo.setTeacherName(teacherTb.getTeacherName());
                classesSubjectTeacherVo.setTeacherAccount(teacherTb.getTeacherAccount());

                //数据设置
                classesSubjectTeacherVos.add(classesSubjectTeacherVo);
            }
        }
        //返回数据

        return ResultPageBuild.pageBuildOk(new ResultPage<ClassesSubjectTeacherVo>(),studentPageVo.getPage(),studentPageVo.getPageSize(),classesSubjectTbResultPage.getCountTotal(),classesSubjectTeacherVos);
    }

    /**
     * 根据学生id查询学生所有的课程考试信息
     * @param studentPageVo
     * @return
     */
    @Override
    public ResultPage<StudentSubjectExaminationVo> queryStudentExaminationByStudentId(StudentPageVo studentPageVo) {
        //根据学生id查询学生信息
        StudentTb studentInfo = studentTbMapper.selectByPrimaryKey(Long.valueOf(studentPageVo.getStudentId()));

        //第一步根据学生id查询班级id
        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        classesStudentTb.setStudentId(studentPageVo.getStudentId());
        Result<ClassesStudentTb> classesStudentTbResult = classesFeign.queryClassesInfoByStudentId(classesStudentTb);
        Integer classesId = ((ClassesStudentTb) classesStudentTbResult.getData()).getClassesId();
        //第二步根据班级id查询班级课程id
        ClassesSubjectPageVo classesSubjectPageVo = new ClassesSubjectPageVo();
        //设置班级id
        classesSubjectPageVo.setClassesId(classesId);
        //设置当前页数
        classesSubjectPageVo.setPage(studentPageVo.getPage());

        //设置取多少个
        classesSubjectPageVo.setPageSize(studentPageVo.getPageSize());
        //这里获取的还有总数
        ResultPage<ClassesSubjectTb> classesSubjectTbResultPage = classesFeign.querySubjectInfoListByClassesId(classesSubjectPageVo);

        //获取所有课程了
        List<ClassesSubjectTb> classesSubjectInfoList = classesSubjectTbResultPage.getData();
        //根据教师课程id查询班级信息
        List<Integer> collectTeacherSubjectIdInfo = classesSubjectInfoList.stream().map((item) -> {
            return item.getTeacherSubjectId();
        }).collect(Collectors.toList());
        ResultPage<TeacherSubjectTb> teacherSubjectTbResultPage = subjectFeign.queryTeacherSubjectInfoByTeacherSubjectId(collectTeacherSubjectIdInfo);
        //获取教师课程id表
        List<TeacherSubjectTb> teacherSubjectTbResultPageData = teacherSubjectTbResultPage.getData();
        List<Integer> collectSubjectIdInfoList = teacherSubjectTbResultPageData.stream().map((item) -> {
            return item.getSubjectId();
        }).collect(Collectors.toList());
        //根据课程id获取课程信息
        ResultPage<SubjectTb> subjectTbResultPage = subjectFeign.queryBatchSubjectInfoBySubjectIdList(collectSubjectIdInfoList);
        //课程信息集合
        List<SubjectTb> subjectTbList = subjectTbResultPage.getData();

        //第四步根据考试id
        ResultPage<ExaminationTb> examinationTbResultPage = examinationFeign.queryExaminationInfoBySubjectId(collectSubjectIdInfoList);
        //考试信息状态
        List<ExaminationTb> examinationInfoList = examinationTbResultPage.getData();

        List<Integer> collectExaminationIdInfoList = examinationInfoList.stream().map((item) -> {
            return item.getExaminationId();
        }).collect(Collectors.toList());
        //查询student_examination_tb
        ResultPage<StudentExaminationTb> studentExaminationTbResultPage = examinationFeign.queryStudentExaminationInfoByStudentIdAndExaminationId(collectExaminationIdInfoList, studentPageVo.getStudentId());
        List<StudentExaminationTb> studentExaminationInfoList = studentExaminationTbResultPage.getData();


        //最后把数据拼接到一起 返回给前端

        //根据课程拼接数据
        ArrayList<StudentSubjectExaminationVo> studentSubjectExaminationVos = new ArrayList<>();
        for (int i = 0; i < subjectTbList.size(); i++) {
            StudentSubjectExaminationVo studentSubjectExaminationVo = new StudentSubjectExaminationVo();
            //设置学生信息

            studentSubjectExaminationVo.setStudentId(studentInfo.getStudentId());
            studentSubjectExaminationVo.setStudentAccount(studentInfo.getStudentAccount());
            studentSubjectExaminationVo.setStudentXueHao(studentInfo.getStudentXueHao());
            studentSubjectExaminationVo.setStudentAccount(studentInfo.getStudentAccount());
            studentSubjectExaminationVo.setStudentName(studentInfo.getStudentName());

            //设置课程信息
            TeacherSubjectTb teacherSubjectTb = teacherSubjectTbResultPageData.get(i);
            SubjectTb subjectTb = subjectTbList.get(i);
            studentSubjectExaminationVo.setSubjectId(subjectTb.getSubjectId());
            studentSubjectExaminationVo.setSubjectName(subjectTb.getSubjectName());

            //考试id
            ExaminationTb examinationTb = examinationInfoList.get(i);
            studentSubjectExaminationVo.setExaminationName(examinationTb.getExaminationName());
            studentSubjectExaminationVo.setExaminationId(examinationTb.getExaminationId());
            studentSubjectExaminationVo.setStudentExaminationId(examinationTb.getExaminationId());

            studentSubjectExaminationVos.add(studentSubjectExaminationVo);
        }
        return ResultPageBuild.pageBuildOk(new ResultPage<StudentSubjectExaminationVo>(),studentPageVo.getPage(),studentPageVo.getPageSize(),classesSubjectTbResultPage.getCountTotal(),studentSubjectExaminationVos);
    }

    /**
     * 根据学生id查询学生所有课程的分数信息
     * @param studentPageVo
     * @return
     */

    @Override
    public ResultPage<StudentSubjectScoreVo> queryStudentSubjectScoreByStudentId(StudentPageVo studentPageVo) {
        Integer studentId = studentPageVo.getStudentId();
        //获取学生信息
        StudentTb studentInfo = studentTbMapper.selectByPrimaryKey(Long.valueOf(studentId));
        //第一步根据学生id查询班级id
        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        classesStudentTb.setStudentId(studentInfo.getStudentId());
        Result<ClassesStudentTb> classesStudentTbResult = classesFeign.queryClassesInfoByStudentId(classesStudentTb);
        Integer classesId = ((ClassesStudentTb) classesStudentTbResult.getData()).getClassesId();
        //第二步根据班级id查询班级课程id
        ClassesSubjectPageVo classesSubjectPageVo = new ClassesSubjectPageVo();
        classesSubjectPageVo.setClassesId(classesId);
        classesSubjectPageVo.setPageSize(studentPageVo.getPageSize());
        classesSubjectPageVo.setPage(studentPageVo.getPage());
        ResultPage<ClassesSubjectTb> classesSubjectTbResultPage = classesFeign.querySubjectInfoListByClassesId(classesSubjectPageVo);
        //班级课程集合
        List<ClassesSubjectTb> classesSubjectInfo = classesSubjectTbResultPage.getData();

        //班级课程集合
        List<Integer> collectTeacherSubjectIdInfoList = classesSubjectInfo.stream().map((item) -> {
            return item.getTeacherSubjectId();
        }).collect(Collectors.toList());

        ResultPage<TeacherSubjectTb> teacherSubjectTbResultPage = subjectFeign.queryTeacherSubjectInfoByTeacherSubjectId(collectTeacherSubjectIdInfoList);

        //收集课程id
        List<TeacherSubjectTb> teacherSubjectTbResultPageData = teacherSubjectTbResultPage.getData();
        List<Integer> collectSubjectIdInfoList = teacherSubjectTbResultPageData.stream().map((item) -> {
            return item.getSubjectId();
        }).collect(Collectors.toList());

        //查出课程信息
        ResultPage<SubjectTb> subjectTbResultPage = subjectFeign.queryBatchSubjectInfoBySubjectIdList(collectSubjectIdInfoList);
        List<SubjectTb>  subjectInfoList = subjectTbResultPage.getData();
        //第三步根据课程id和学生id查询分数

        //获取学生分数
        ResultPage<ScoreTb> scoreTbResultPage = scoreFeign.queryStudentScoreBySubjectIdAndStudentId(collectSubjectIdInfoList, studentId);

        List<ScoreTb> scoreInfoList = scoreTbResultPage.getData();

        ArrayList<StudentSubjectScoreVo> studentSubjectScoreVos = new ArrayList<>();
        //最后把数据拼接到一起返回给前端
        for (int i = 0; i < subjectInfoList.size(); i++) {
            //数据拼接
            StudentSubjectScoreVo studentSubjectScoreVo = new StudentSubjectScoreVo();
            //获取课程
            SubjectTb subjectTb = subjectInfoList.get(i);
            //获取分数
            ScoreTb scoreTb = scoreInfoList.get(i);

            //学生信息赋值
            studentSubjectScoreVo.setStudentId(studentInfo.getStudentId());
            studentSubjectScoreVo.setStudentName(studentInfo.getStudentName());
            studentSubjectScoreVo.setStudentXueHao(studentInfo.getStudentXueHao());
            studentSubjectScoreVo.setStudentAccount(studentInfo.getStudentAccount());

            //分数赋值
            studentSubjectScoreVo.setScore(scoreTb.getScore());
            studentSubjectScoreVo.setScoreId(scoreTb.getScoreId());
            //课程赋值
            studentSubjectScoreVo.setSubjectId(subjectTb.getSubjectId());
            studentSubjectScoreVo.setSubjectName(subjectTb.getSubjectName());

            studentSubjectScoreVos.add(studentSubjectScoreVo);
        }



        return ResultPageBuild.pageBuildOk(new ResultPage<StudentSubjectScoreVo>(), classesSubjectTbResultPage.getPage(),classesSubjectTbResultPage.getPageSize(),classesSubjectTbResultPage.getCountTotal(),studentSubjectScoreVos);
    }

    @Override
    public ResultPage<StudentTb> queryBatchStudentInfoByStudentIdList(List<Integer> studentIdList) {
        //通过学生id批量查询学生信息

        List<StudentTb> studentTbList = studentTbMapper.queryBatchStudentInfoByStudentIdList(studentIdList);
        ResultPage<StudentTb> resultPage = new ResultPage<>();
        resultPage.setData(studentTbList);
        return resultPage;
    }
    /***
     *
     *
     * 不知道哪里写错了，头疼，重写
     *
     *  @Override
     *     public ResultPage<StudentInfoVo> queryClassesStudentListInfoByStudentId(@RequestBody StudentPageVo studentPageVo) {
     *         ClassesStudentTb classesStudentTb = new ClassesStudentTb();
     *         classesStudentTb.setStudentId(studentPageVo.getStudentId());
     *         System.out.println(classesStudentTb);
     *
     *         //第一步调用classesFeign查询出学生的id所在的班级 然后 在查班级的学生id
     *         ResultPage<ClassesStudentTb> classesStudentTbResultPage = classesFeign.queryClassesStudenInfotByStudentId(studentPageVo.getStudentId());
     * //        ResultPage<ClassesStudentTb> classesStudentTbResultPage = classesFeign.queryClassesStudenInfotByStudentId(classesStudentPageVo1);
     *         //学生所在班级id
     *         List<ClassesStudentTb> data1 = classesStudentTbResultPage.getData();
     *           data1.get(0).getClassesId();
     *
     *
     *         //根据班级id查询数据
     *         //创建班级页Vo
     *         ClassesStudentPageVo classesStudentPageVo = new ClassesStudentPageVo();
     *         classesStudentPageVo.setClassesId(classesId);
     *         //设置页面数据
     *         classesStudentPageVo.setPage(studentPageVo.getPage());
     *         //设置页面大小
     *         classesStudentPageVo.setPageSize(studentPageVo.getPageSize());
     *         //查询班级所有的信息
     *         ResultPage<ClassesStudentTb> query = classesFeign.queryClassesStudent(classesStudentPageVo);
     *         //查询班级成员的总数
     *         int countTotal = query.getCountTotal();
     *
     *         List<ClassesStudentTb> data = query.getData();
     *         System.out.println("查询数据的结果为："+data);
     *         //获取学生id
     *         List<Integer> collectStudentIdList = data.stream().map((item) -> {
     *             Integer studentId = item.getStudentId();
     *             return studentId;
     *
     *         }).collect(Collectors.toList());
     *         //最后根据返回的数据查询学生成员的数据 然后返回个前端
     *         System.out.println(collectStudentIdList);
     *         //调用Student批量查询id插入数据的Mapper
     *         List<StudentTb> studentTbList = studentTbMapper.queryBatch(collectStudentIdList);
     *
     *         //密码选项置为空
     *         List<StudentInfoVo> collectStudentInfo = studentTbList.stream().map(
     *                 (item) -> {
     *                     StudentInfoVo studentInfoVo = new StudentInfoVo();
     *                     //数据拷贝
     *                     BeanUtils.copyProperties(item, studentInfoVo);
     *                     return studentInfoVo;
     *                 }
     *         ).collect(Collectors.toList());
     *
     *         //返回给前端
     *         return ResultPageBuild.pageBuildOk(new ResultPage<StudentInfoVo>(),studentPageVo.getPage(),studentPageVo.getPageSize(),countTotal,collectStudentInfo);
     *     }
     */

    //服务
}
